package com.autumn.zero.workflow.entitys;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.apache.ibatis.type.JdbcType;
import org.hibernate.validator.constraints.Length;

import com.autumn.common.api.constants.BasiscArchivesStatusConstant;
import com.autumn.constants.SettingConstants;
import com.autumn.domain.entities.IStatusEntity;
import com.autumn.domain.entities.auditing.AbstractDefaultModifiedGmtEntity;
import com.autumn.mybatis.mapper.annotation.ColumnOrder;
import com.autumn.mybatis.mapper.annotation.ColumnType;
import com.autumn.mybatis.mapper.annotation.Index;
import com.autumn.util.ExceptionUtils;
import com.autumn.validation.annotation.NotNullOrBlank;

/**
 * 工作流表单
 * 
 * @author 老码农 2018-12-19 22:47:16
 */
@Table(name = SettingConstants.SYS_TABLE_PREFIX + "_workflow_form")
public class WorkflowForm extends AbstractDefaultModifiedGmtEntity implements IStatusEntity<Long> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6836839726988252075L;

	/**
	 * 最大名称长度
	 */
	public static final int MAX_FORM_TYPE_NAME_LENGTH = 50;

	/**
	 * 最大摘要长度
	 */
	public static final int MAX_SUMMARY_LENGTH = 255;

	/**
	 * 字段 formType
	 */
	public static final String FILED_FORM_TYPE = "formType";

	/**
	 * 字段 formName
	 */
	public static final String FILED_FORM_NAME = "formName";

	/**
	 * 字段 summary
	 */
	public static final String FILED_SUMMARY = "summary";

	/**
	 * 表单类型
	 */
	@NotNull(message = "表单类型不能为空")
	@Column(name = "form_type", nullable = false)
	@ColumnOrder(1)
	@Index(unique = true)
	private Integer formType;

	@NotNullOrBlank(message = "表单名称不能为空")
	@Length(max = MAX_FORM_TYPE_NAME_LENGTH, message = "表单名称长度不能超过" + MAX_FORM_TYPE_NAME_LENGTH + "个字。")
	@Column(name = "form_name", nullable = false, length = MAX_FORM_TYPE_NAME_LENGTH)
	@ColumnType(jdbcType = JdbcType.VARCHAR)
	@ColumnOrder(2)
	@Index(unique = true)
	private String formName;

	/**
	 * 
	 */
	@NotNull(message = "状态不能为空")
	@Column(name = "status", nullable = false)
	@ColumnOrder(3)
	private Integer status;

	/**
	 * 摘要
	 */
	@Length(max = MAX_SUMMARY_LENGTH, message = "摘要长度不能超过" + MAX_SUMMARY_LENGTH + "个字。")
	@Column(name = "summary", nullable = false, length = MAX_SUMMARY_LENGTH)
	@ColumnType(jdbcType = JdbcType.VARCHAR)
	@ColumnOrder(4)
	private String summary;

	/**
	 * 属性集合
	 */
	private List<WorkflowFormProperty> propertys;

	/**
	 * 
	 */
	public WorkflowForm() {
		this.setPropertys(new ArrayList<>());
	}

	/**
	 * 获取表单类型
	 * 
	 * @return
	 */
	public Integer getFormType() {
		return formType;
	}

	/**
	 * 设置表单类型
	 * 
	 * @param formType 表单类型
	 */
	public void setFormType(Integer formType) {
		this.formType = formType;
	}

	/**
	 * 获取表单名称
	 * 
	 * @return
	 */
	public String getFormName() {
		return formName;
	}

	/**
	 * 设置表单名称
	 * 
	 * @param formName 表单类型名称
	 */
	public void setFormName(String formName) {
		this.formName = formName;
	}

	@Override
	public Integer getStatus() {
		return status;
	}

	@Override
	public void setStatus(Integer status) {
		this.status = status;
	}

	/**
	 * 获取摘要
	 * 
	 * @return
	 */
	public String getSummary() {
		return summary;
	}

	/**
	 * 设置摘要
	 * 
	 * @param summary 摘要
	 */
	public void setSummary(String summary) {
		this.summary = summary;
	}

	/**
	 * 获取属性集合
	 * 
	 * @return
	 */
	public List<WorkflowFormProperty> getPropertys() {
		return propertys;
	}

	/**
	 * 设置属性集合
	 * 
	 * @param propertys
	 */
	public void setPropertys(List<WorkflowFormProperty> propertys) {
		this.propertys = propertys;
	}

	@Override
	public void valid() {
		super.valid();
		if (this.getPropertys() == null) {
			this.setPropertys(new ArrayList<>());
		}
		if (!BasiscArchivesStatusConstant.exist(this.getStatus())) {
			ExceptionUtils.throwValidationException("无效的表单状态。");
		}
		Set<String> propertyNameSet = new HashSet<>();
		Set<String> propertyFriendlySet = new HashSet<>();
		try {
			for (WorkflowFormProperty property : this.getPropertys()) {
				property.forNullToDefault();
				property.valid();
				if (!propertyNameSet.add(property.getPropertyName().toLowerCase())) {
					ExceptionUtils.throwValidationException("同一表单，属性名称[" + property.getPropertyName() + "]不能重服。");
				}
				if (!propertyFriendlySet.add(property.getFriendlyName().toLowerCase())) {
					ExceptionUtils.throwValidationException("同一表单，属性友好名称[" + property.getFriendlyName() + "]不能重服。");
				}
			}
		} finally {
			propertyNameSet.clear();
			propertyFriendlySet.clear();
			propertyNameSet = null;
			propertyFriendlySet = null;
		}
	}
}
